<?php
include '../conn.php';
include 't-cookie.php';

$method = $_POST['method'];
$ProjectId = $_POST['ProjectId'];
$StudentId = $_POST['StudentId'];
$TeacherId = $_SESSION['t-id'];
$Year = $_SESSION['t-Year'];
echo $date = $_POST['date'];
if ($method == "catch") {

    $selectsql = "select * from  probyself WHERE StudentId=$StudentId";
    $result = mysqli_query($conn, $selectsql);
    $row = mysqli_fetch_array($result);
    $sql = "insert into project  VALUES ('','$row[1]','$row[2]','$row[3]','$row[4]','$row[5]',null,null,'$TeacherId','$row[6]','1','$date')";
    if ($conn->query($sql)) {
        $sql = "update probyself set activite='1' where ProjectId=$ProjectId";
        if ($conn->query($sql)) {
            $addtsql = "update student set Teacher_TeacherId=$TeacherId WHERE StudentId=$StudentId";
            if ($conn->query($addtsql)) {
                echo("添加自拟课题和学生成功");
                //添加到指导表
                //计算guidemark最大id
                $sql = "select max(GuideMarkId) from guidemark ";
                $result = mysqli_query($conn, $sql);
                $row = mysqli_fetch_row($result);
                $GuideMarkId = $row[0] + 1;
                //添加到指导表
                $pidsql = "select ProjectId from project WHERE Student_StudentId=$StudentId";
                $pidresult = $conn->query($pidsql);
                $pidrow = $pidresult->fetch_array();
                $addguidemark = "insert into guidemark(GuideMarkId,Year,Order1,Order2,Order3,Order4,Order5,remark,Project_ProjectId) values
('$GuideMarkId','$Year',NULL,NULL,NULL,NULL,NULL,NULL,'$pidrow[0]')";
                if ($conn->query($addguidemark)) {
                    echo "添加指导表成功 </br>";
                } else {
                    echo "添加指导表失败  </br>";
                }
            } else {
                echo("未添加该学生进管理资料");
            }
        } else {
            echo("无法添加课题");
        }
    } else {
        echo("添加自拟课题和学生失败");
    }
}
if ($method == "choose") {
    $sql = "update project set Student_StudentId='$StudentId',activate='1',time='$date' WHERE ProjectId=$ProjectId ";
    if ($conn->query($sql)) {
        $dsql = "update choosesubject set state='2' WHERE ProjectId=$ProjectId";
        $fsql = "update choosesubject set state='1' WHERE ProjectId=$ProjectId and StudentId=$StudentId";
        if ($conn->query($dsql)) {
            if ($conn->query($fsql)) {
                $addtsql = "update student set Teacher_TeacherId=$TeacherId WHERE StudentId=$StudentId";
                if ($conn->query($addtsql)) {
                    echo("添加学生成功");
                    //添加到指导表
                    //计算guidemark最大id
                    $sql = "select max(GuideMarkId) from guidemark ";
                    $result = mysqli_query($conn, $sql);
                    $row = mysqli_fetch_row($result);
                    $GuideMarkId = $row[0] + 1;
                    //添加到指导表
                    $addguidemark = "insert into guidemark(GuideMarkId,Year,Order1,Order2,Order3,Order4,Order5,remark,Project_ProjectId) values
('$GuideMarkId','$Year',NULL,NULL,NULL,NULL,NULL,NULL,'$ProjectId')";
                    if ($conn->query($addguidemark)) {
                        echo "添加指导表成功 </br>";
                    } else {
                        echo "添加指导表失败  </br>";
                    }
                } else {
                    echo("未添加该学生进管理资料");
                }
            } else {
                echo("选题修改失败");
            }
        } else {
            echo("所有选题修改失败");
        }
    } else {
        echo("添加学生失败");
    }
}

